<%@ page import="com.ffpc.struts.StrutsMessage"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.net.URLEncoder"%>
<%@ page import="java.net.URLDecoder"%>
<%@ page import="admin.*"%>
<%@ page import="com.ffpc.sql.*"%>

<%@ include file="connect.jsp"%>


<LINK rel='stylesheet' type='text/css' href='<%=contextPath%>/style.css'>

<head>
<title>Hutchison - FAS Application</title>
<!-- by Stephen Lee -->
<meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-store">
<!-- by Stephen Lee -->

</head>
<%
 // 12-Nov-2012 
   if (user == null) {
      response.sendRedirect("../tssLogout.jsp");
      return;
    }
%>    
<table width=100% cellpadding=0 cellspacing=0>
	<tr>
		<td align=left style="color: white; background-color: #00AADD;">
		* - Mandatory Field</td>
		<td align=right style="color: white; background-color: #00AADD;">Welcome
		&nbsp; &nbsp;<b><i><%=user.getUser_name()%> - <%=user.getCost_center_id()%></i></b></td>
	</tr>
</table>

<script lang=javascript>
    var msg = '<%= message.getMessage() %>';
    if (msg > "") 
        {
        alert(msg);
        <% 
        message.setMessage("");
        request.setAttribute("message", message);
        %>
        }
</script>

<script language=javascript>

	function Tokenizer(string, separator, pos) {
		var oText = string;
		var nText = null;
		for ( var i = 0; i < pos; i++) {
			if (oText.indexOf(separator) == -1) {
				nText = oText;
			} else {
				nText = oText.substring(0, oText.indexOf(separator));
				oText = oText.substring(oText.indexOf(separator) + 1);
			}
		}
		return nText;
	}

	function isNull(object, desc) {
		var obj = object;
		if (obj.value == null || obj.value.length == 0) {
			alert(desc + " cannot be null! ");
			return true;
		}
		return false;
	}

	function isInt(object) {
		var obj = object;
		if (parseInt(obj.value) == parseFloat(obj.value)) {
			return true;
		}
		return false;
	}

	function isNotNumeric(object, desc) {
		var obj = object;

		if (obj.value != null && isNaN(obj.value)) {
			alert(desc + "  must be numeric!");
			return true;
		}
		return false;
	}
			
	// CR1054 This is a more comprehensive function for date validation
	// and can be used to replace isDate() below

	function validationDate(dateStr) {

		var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
		var matchArray = dateStr.match(datePat);

		if (matchArray == null) {
			return false;
		}

		day = matchArray[1];
		month = matchArray[3];
		year = matchArray[5];

		if (day < 1 || day > 31) {
			return false;
		}

		if (month < 1 || month > 12) {
			return false;
		}

		if ((month == 4 || month == 6 || month == 9 || month == 11)
				&& day == 31) {
			return false;
		}

		if (month == 2) {
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day > 29 || (day == 29 && !isleap)) {
				return false;
			}
		}
		return true;
	}

	function checkDate(d, desc) {
		if (d.value != "") {
			if (! validationDate(d.value)) {
				alert(desc + " '" + d.value + "' has invalid date format (DD/MM/YYYY).");
				return false;
			}
		}
		return true;
	}
	
	function isDate(object, desc) {
		var obj = object;
		var d = obj.value;
		if (chLen(obj, " date field (dd/MM/yyyy) ", 10) == false) {
			alert(desc + " has invalid date format.");
			return false;
		}
		if (d.substring(2, 3) != "/" || d.substring(5, 6) != "/") {
			alert(desc + " has invalid date format.");
			return false;
		}
		var day = d.substring(0, 2);
		var month = d.substring(3, 5);
		var year = d.substring(6, 10);
		if (day == null || isNaN(day)) {
			alert(desc + " has an invalid day.");
			return false;
		}
		if (month == null || isNaN(month)) {
			alert(desc + " has an invalid month.");
			return false;
		}
		if (parseInt(day) > 31) {
			alert(desc + " has an invalid day.");
			return false;
		}
		if (parseInt(month) > 12) {
			alert(desc + " has an invalid month.");
			return false;
		}
		if (!validationDate(d)) {
			alert(desc + " is not an valid date.");
			return false;
		}

		return true;
	}

	function addNewRowBefore(targetTable, newRowTemplate, beforeRow) {
		var newRow = targetTable.insertRow(beforeRow);
		for ( var i = 0; i < newRowTemplate.rows(0).cells.length; i++) {
			newRow.insertCell().innerHTML = newRowTemplate.rows(0).cells(i).innerHTML;
		}
		return newRow;
	}

	function delRow(targetTable, rownum) {
		targetTable.deleteRow(rownum);
	}

	var EARLIER = -1;
	var SAME = 0;
	var LATER = 1;

	function convertDate(dateString) {
		var iDay = parseInt(dateString.substr(0, 2), 10);
		var iMonth = parseInt(dateString.substr(3, 2), 10) - 1;
		var iYear = parseInt(dateString.substr(6, 4), 10);

		return (new Date(iYear, iMonth, iDay));
	}

	function compareDate(date1, date2) {

		if (date1 - date2 < 0)
			return EARLIER;
		else if (date1 - date2 > 0)
			return LATER;
		else
			return SAME;
	}
	
 function Tokenizer(string, separator, pos)
 {
    var oText = string;
    var nText = null;
    for (var i=0; i < pos; i++)
    {
       if (oText.indexOf(separator)==-1)
       {
          nText = oText
       }
       else
       {
          nText = oText.substring(0, oText.indexOf(separator));
          oText = oText.substring(oText.indexOf(separator)+1);
       }
    }
    return nText;
 }

 function isNull(object,desc) {
    var obj=object;
    if (obj.value==null||obj.value.length==0) {
       alert(desc + " cannot be null! ");
       return true;
    }
    return false;
 }

 function isInt(object) {
    var obj=object;
    if (parseInt(obj.value)==parseFloat(obj.value)) {
      return true;
    }
      return false;
 }
 function isNotNumeric(object,desc) {
    var obj=object;

    if (obj.value!=null && isNaN(obj.value)) {
       alert(desc + "  must be numeric!");
       return true;
    }
    return false;
 }

// CR1054 This is a more comprehensive function for date validation
// and can be used to replace isDate() below

 function validationDate(dateStr) {

    var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
    var matchArray = dateStr.match(datePat);

    if (matchArray == null) {
        return false;
    }

    day = matchArray[1];
    month = matchArray[3];
    year = matchArray[5];

    if (day < 1 || day > 31) {
        return false;
    }

    if (month < 1 || month > 12) {
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        return false;
    }

    if (month == 2) {
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            return false;
        }
    }
    return true;
}

 function isDate(object,desc) {
    var obj=object;
    var d = obj.value;
    if (chLen(obj," date field (dd/MM/yyyy) ",10)==false)
       return false;
    if (d.substring(2,3)!="/" || d.substring(5,6)!="/") {
       alert(" Invalid Date Format.");
       return false;
    }
    var day = d.substring(0,2);
    var month = d.substring(3,5);
    var year = d.substring(6,10);
    if (day==null || isNaN(day)) {
       alert(" Invalid Date Format.");
       return false;
    }
    if (month==null || isNaN(month)) {
       alert(" Invalid Date Format.");
       return false;
    }
    if (day==null || isNaN(day)) {
       alert(" Invalid Date Format.");
       return false;
    }
    if (parseInt(day)>31) {
       alert(" Invalid Date Format.");
       return false;
    }
    if (parseInt(month)>12) {
       alert(" Invalid Date Format.");
       return false;
    }
    return true;
 }


 function chLen(object,desc,len) {
    var obj=object;

    if (obj.value!=null && obj.value.length!=len) {
       alert("The field length of " + desc + "  must be " + len + "! ");
       return false;
    }
    return true;
 }

 function deleteRecord() {
    if (fm.formAction.value!="Update")
    {
          alert('This is a new record.');
          return;
    }
    if (confirm("Are you sure to delete this record?"))
    {

       fm.formAction.value="Delete";
       fm.submit();
    }
 }

 function save() {
    if (confirm("Are you sure to save this record ?"))
    {
       if (!formValidation()) {
          return false;
       }
       fm.submit();
    }
 }

 function search() {
    if (!formValidation())
       return;
    fm.submit();
 }

 function spsubmit() {
    if (confirm("Are you sure to submit this request ?"))
    {
       if (!formValidation())
          return;
       fm.submit();
    }
 }

 function addNewRow(targetTable,newRowTemplate){
    var newRow = targetTable.insertRow();
    for(var i=0; i<newRowTemplate.rows(0).cells.length; i++){
      newRow.insertCell().innerHTML = newRowTemplate.rows(0).cells(i).innerHTML;
    }
    return newRow;
 }

 function delRows(targetTable,startRow,checkCellIndex,checkChildIndex,statusCellIndex,statusChildIndex,statusForDelete,deleteStatus) {
  if (confirm("Are you to delete this record ?")) {
    var checkCell   = checkCellIndex;
    var checkChild  = checkChildIndex;
    var statusCell  = statusCellIndex;
    var statusChild = statusChildIndex;
    for (var i=startRow; i<targetTable.rows.length; i++ ) {
      if (targetTable.rows(i).cells(checkCell).children(checkChild).checked) {
         if (targetTable.rows(i).cells(statusCell).children(statusChild).value!=statusForDelete) {
            targetTable.deleteRow(i);
            i = i - 1;
         } else {
           targetTable.rows(i).style.display = 'none';
           targetTable.rows(i).cells(statusCell).children(statusChild).value = deleteStatus;
         }
      }
    }
  }
 }

 function delAllRows(targetTable) {
    for (var i=0; i<targetTable.rows.length; i++ ) {
        targetTable.deleteRow(i);
        i = i - 1;
    }
 }

 function delRow(targetTable,rownum) {
    targetTable.deleteRow(rownum);
 }

 function encode(s) {
    t="";
    for (var i=0;i<s.length;++i) {
        ch=s.charAt(i);
        if (ch=="/") t +="//"
        else if (ch==" ") t += "/b"
        else if (ch==",") t += "/."
        else if (ch==";") t += "/:"
        else if (ch=="\n") t += "/n"
        else if (ch=="\r") t += "/r"
        else if (ch=="\t") t += "/t"
        else if (ch=="\b") t += "/b"
        else t += ch;
    }
    return t;
 }

 function decode(s) {
    t="";
    if (s==null) return;
    for (var i=0;i<s.length;++i) {
        ch=s.charAt(i);
        if (ch=="/") {
           ++i;
           if (i<s.length) {
              ch=s.charAt(i);
              if (ch=="/") t += ch
              else if (ch==".") t += ","
              else if (ch==":") t += ";"
              else if (ch=="n") t += "\n"
              else if (ch=="r") t += "\r"
              else if (ch=="t") t += "\t"
              else if (ch=="b") t += " "
           }
        } else t += ch
    }
    return t;
 }
//CR99999 You can use the following function(getRightNum) to format a real number,
//by giving three parameters.
//(1).number : a real number
//(2).decimal: the precision you want.
//(3).desc:    if error throw a tip.
function getRightNum(number,decimal,desc)
{
        number=TrimAll(number);
        if (decimal<0) return "";
    if(number=="")
        {
                return "";
        }

        var isN=isNaN(number);
        var index=number.indexOf('.');
        var len=number.length;

        if (isN)
        {
        alert("Invalid "+desc);
                return "";
        }

        //heandle when decimal is 0, that is you need a integer.
        if((decimal==0)&&(index==-1))
                return number;
        if((decimal==0)&&(index==0))
                return "0";
        if((decimal==0)&&(index>0))
                return number.substring(0,index);
    //cut some unwanted.
        if((index!=-1)&&(index<=len-decimal-1))
                return number.substring(0,index+decimal+1);
    //if a number ,and decimal is not 0,do this.
        if(index==-1)
        {
                number+=".";
            for (i=0;i<decimal;i++)
                number+="0";
            return number;
        }
        //fill enough '0' on the right of the number
        var count=decimal+index+1-len;
    for (i=0;i<count;i++)
                number+="0";
        return number;

}
function LTrim(sString)// throw off the left blanks of the String.
{
var sStr,i,iStart,sResult = "";

sStr = sString.split("");
iStart = -1 ;
for (i = 0 ; i < sStr.length ; i++)
{
if (sStr[i] != " ")
{
iStart = i;
break;
}
}
if (iStart == -1) { return "" ;}    //if the String are all blanks,return "".
else { return sString.substring(iStart) ;}
}

function RTrim(sString)// throw off the right blanks of the String.
{
var sStr,i,sResult = "",sTemp = "" ;

sStr = sString.split("");
for (i = sStr.length - 1 ; i >= 0 ; i --)  // reverse the String
{
sResult = sResult + sStr[i];
}
sTemp = LTrim(sResult) ; // throw off the left blanks of the String.

if (sTemp == "") { return "" ; }

sStr = sTemp.split("");
sResult = "" ;
for (i = sStr.length - 1 ; i >= 0 ; i--) // reverse the String again.
{
sResult = sResult + sStr[i];
}
return sResult ;
}

function TrimAll(sString)// throw off the left and right blanks of the String.
{
var strTmp ;

strTmp = RTrim(LTrim(sString)) ;

return strTmp ;
}

</script>

